package com.ls.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ls.controller.startbanner.model.AddSBannerModel;
import com.ls.controller.startbanner.model.EditSBannerModel;
import com.ls.controller.startbanner.model.updateStatusModel;
import com.ls.entity.StartBanner;
import com.ls.mapper.StartBannerMapper;
import com.ls.service.IStartBannerService;
import com.ls.utils.ResultData;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

import static com.ls.constant.SystemConstants.NUMBER_0;
import static com.ls.constant.SystemConstants.NUMBER_1;

@Service
@Slf4j
@RequiredArgsConstructor
public class StartBannerServiceImpl extends ServiceImpl<StartBannerMapper, StartBanner> implements IStartBannerService {
    /**
     * app轮播图
     */
    @Override
    public ResultData getBanner() {
        StartBanner startBanner = getOne(new LambdaQueryWrapper<StartBanner>().eq(StartBanner::getValidFlag, NUMBER_1));
        return ResultData.successData(startBanner);
    }

    /**
     * 新增轮播图
     */
    @Override
    public ResultData add(AddSBannerModel model) {
        StartBanner startBanner = new StartBanner();
        BeanUtils.copyProperties(model, startBanner);
        startBanner.setValidFlag(NUMBER_1);
        //将数据库所有轮播图设为无效
        update(new LambdaUpdateWrapper<StartBanner>()
                .set(StartBanner::getValidFlag, NUMBER_0));
        return save(startBanner) ?
                ResultData.successMsg("新增成功") :
                ResultData.failure("新增失败");
    }
    /** 编辑轮播图 */
    @Override
    public ResultData edit(EditSBannerModel model) {
        StartBanner startBanner = new StartBanner();
        BeanUtils.copyProperties(model, startBanner);
        startBanner.setUpdateTime(LocalDateTime.now());
        return updateById(startBanner) ?
                ResultData.successMsg("修改成功") :
                ResultData.failure("修改失败");
    }
    /** 删除轮播图  */
    @Override
    public ResultData del(Integer sBannerId) {
        StartBanner startBanner = getById(sBannerId);
        if (startBanner == null){
            return ResultData.failure("轮播图不存在");
        }
        return startBanner.deleteById() ?
                ResultData.successMsg("删除成功") :
                ResultData.failure("删除失败");
    }
    /** app轮播图列表 */
    @Override
    public ResultData pageList() {
        List<StartBanner> list = list(new LambdaQueryWrapper<StartBanner>().orderByDesc(StartBanner::getCreateTime));
        return ResultData.successData(list);
    }
    /** 修改轮播图启用状态 */
    @Override
    public ResultData updateStatus(updateStatusModel model) {
        StartBanner startBanner = getById(model.getSBannerId());
        if (startBanner == null){
            return ResultData.failure("轮播图不存在");
        }
        startBanner.setValidFlag(model.getValidFlag());
        startBanner.setUpdateTime(LocalDateTime.now());
        update(new LambdaUpdateWrapper<StartBanner>()
                .ne(StartBanner::getSBannerId, model.getSBannerId())
                .set(StartBanner::getValidFlag, NUMBER_0));
        return updateById(startBanner) ?
                ResultData.successMsg("修改成功") :
                ResultData.failure("修改失败");
    }

}
